<?php
	require_once ('checks.php');
	require_once('Database.php');
	require_once ('Image.php');
	$db = new DB();
	$imagearr = $db->takeGallery();
	$n = count($imagearr);
	
	
	if (isset($_POST['Upload'])) {		
		$allowedExts = array("gif", "jpeg", "jpg", "png");
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		if ((		($_FILES["file"]["type"] == "image/gif")
				|| 	($_FILES["file"]["type"] == "image/jpeg")
				|| 	($_FILES["file"]["type"] == "image/jpg")
				|| 	($_FILES["file"]["type"] == "image/pjpeg")
				|| 	($_FILES["file"]["type"] == "image/x-png")
				|| 	($_FILES["file"]["type"] == "image/png"))
				&& ($_FILES["file"]["size"] < 20971520)
				&& in_array($extension, $allowedExts)){
		  if ($_FILES["file"]["error"] > 0){
	 	    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		    } else {
			    if (file_exists("images/gallery/" . $_FILES["file"]["name"])){
			      echo $_FILES["file"]["name"] . " already exists. ";
			      } else {
			      move_uploaded_file($_FILES["file"]["tmp_name"],"images/gallery/" . $_FILES["file"]["name"]);
			      
			      

			      if (isset($_POST['fldcheckbox'])) {
			      	$flag="1";
			      } else {
			      	$flag="0";
			      }
			      
			      
			      
			      $db= new DB();
			      $img= new Image($_FILES["file"]["name"],$_POST['gallery'],$_POST['date'],$flag,$_POST['order']);
			      $db->addImage($img);
			      }
		    }
		  } else {
		 	 echo "Invalid file";
		  }
	}
?>
<form method="post" action="?page=admin_upload_image" enctype="multipart/form-data">
			<table>
				<tr>
					<td><label for="file">File Upload: </label>
					</td>
					<td colspan="2"><input type="file" name="file" id="file"><br>
					</td>
				</tr>
				
					<tr>
						<td>Front Page :
						</td>
						<td> 
							<input type="checkbox" name="fldcheckbox" id="fldcheckbox" />
						</td>
					</tr>
					<tr>
						<td>
							Order :
						</td>
						<td>
							<input type="text" name="order" id="order" size="2" value="<?php echo $n+1; ?>">
						</td>	
					</tr>
					<tr>
						<td> Date :</td>
						<td colspan="2">
							<input type="text" class="tcal" name="date" maxlength="10">
						</td>
					</tr>
					<tr>
						<td colspan = "2">
							<textarea name="gallery" id="gallery" cols="20" rows="20" >
						</textarea>
						</td>
					</tr>
				
				<tr>
					
					<td colspan='2' >
					<input type="submit" value="Upload" name="Upload">
						<input type="button" value="Home"  onclick="location.href='?page=admin_index'">
					</td>				
				</tr>			
			</table>
		</form>